﻿using UnityEngine;
using System.Collections;

// bullet
public class Bullet : MonoBehaviour 
{
    public float m_velocity;
    public float m_energy;
    public float m_lifeTime;

    protected Vector2 m_speed;
    protected float m_timer;

	// Use this for initialization
	void Start () 
    {
        m_timer = 0.0f;
	}
	
	// Update is called once per frame
	void Update () 
    {
        Vector3 pos = transform.localPosition;

        pos.x += m_speed.x * Time.fixedDeltaTime;
        pos.y += m_speed.y * Time.fixedDeltaTime;

        transform.localPosition = pos;

        m_timer += Time.fixedDeltaTime;

        if (m_timer >= m_lifeTime)
        {
            Destroy(gameObject);
        }
	}

    /// <summary>
    /// collision with enemy 
    /// </summary>
    /// <param name="coll"></param>
    void OnCollisionEnter2D(Collision2D coll)
    {
        Destroy(gameObject);
    }

    /// <summary>
    /// set dir 
    /// </summary>
    /// <param name="dir"></param>
    public void SetDir(Vector2 dir)
    {
        float angle = Vector2.Angle(Vector2.up, dir);
        if (dir.x > 0)
        {
            angle = -angle;
        }

        transform.localRotation = Quaternion.Euler(0, 0, angle);

        // calculate speed 
        m_speed = new Vector2(m_velocity * dir.x, m_velocity * dir.y);
    }

}
